/**************************************************************************************************

	NSurvey - The web survey and form engine
	Copyright (c) 2004, Thomas Zumbrunn. (http://www.nsurvey.org)

	Dotnetnuke PA module 
	Copyright (c) 2004, Oliver Hine (oliver.hine@gmail.com)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


************************************************************************************************/
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Votations.NSurvey.WebAdmin.UserControls;
using Votations.NSurvey;
using Votations.NSurvey.Data;
using Votations.NSurvey.DataAccess;
using Votations.NSurvey.BusinessRules;
using DotNetNuke;

namespace Votations.NSurvey.WebAdmin
{
	public class SurveyPrivacy: PageBase
	{
		protected System.Web.UI.WebControls.Literal PrivacySettingsTitle;
		protected System.Web.UI.WebControls.Literal RedirectionURLLabel;
		protected System.Web.UI.WebControls.TextBox RedirectionURLTextBox;
		protected System.Web.UI.WebControls.Literal ThanksMessageLabel;
		protected DotNetNuke.UI.UserControls.TextEditor ThankYouFreeTextBox;
		protected System.Web.UI.WebControls.Literal Literal4;
		protected System.Web.UI.WebControls.Button ApplyPrivacyButton;
		protected HeaderControl Header;
		protected SurveyListControl SurveyList;
		protected System.Web.UI.WebControls.Literal ThanksMessageConditionTitle;
		protected System.Web.UI.WebControls.Label MessageLabel;
		protected System.Web.UI.WebControls.Label EvaluationMessageConditionInfo;
		protected System.Web.UI.WebControls.HyperLink AddNewConditionHyperLink;
		protected SurveyMessageConditionsControl SurveyMessageConditons;

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.ApplyPrivacyButton.Click += new System.EventHandler(this.ApplyPrivacyButton_Click);
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion

		private void Page_Load(object sender, System.EventArgs e)
		{
			try
			{
				SetupSecurity();
				LocalizePage();
				SurveyMessageConditons.SurveyId = SurveyId;

				if (!Page.IsPostBack)
				{
					SurveyList.BindDropDown();
					Header.SurveyId = SurveyId; 
					BindFields();
				}
			}
			catch( Exception exc)
			{
				DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this,exc);
			}
		}

		private void SetupSecurity()
		{
			CheckRight(NSurveyRights.AccessPrivacySettings, true);
		}

		private void LocalizePage()
		{
			PrivacySettingsTitle.Text = GetPageResource("PrivacySettingsTitle");
			RedirectionURLLabel.Text  = GetPageResource("RedirectionURLLabel");
			ApplyPrivacyButton.Text = GetPageResource("ApplyPrivacyButton");
			ThanksMessageConditionTitle.Text = GetPageResource("ThanksMessageConditionTitle");
			AddNewConditionHyperLink.Text = GetPageResource("AddNewConditionHyperLink");
			AddNewConditionHyperLink.NavigateUrl = string.Format("{0}&surveyid={1}&menuindex={2}",
				UINavigator.MessageConditionEditorHyperLink, SurveyId, MenuIndex);
			EvaluationMessageConditionInfo.Text = GetPageResource("EvaluationMessageConditionInfo");
			ThanksMessageLabel.Text = GetPageResource("ThanksMessageLabel");
		}

		/// <summary>
		/// Get the current DB data and fill 
		/// the fields with them
		/// </summary>
		private void BindFields()
		{
			SurveyList.SurveyId = SurveyId;
			SurveyList.BindDropDown();

			// Retrieve the survey data
			SurveyData surveyData = new Surveys().GetSurveyById(SurveyId);
			SurveyData.SurveysRow survey = surveyData.Surveys[0];

			RedirectionURLTextBox.Text = survey.RedirectionURL;
			ThankYouFreeTextBox.Text = survey.ThankYouMessage;

			SurveyMessageConditons.BindData();
		}

		private void ApplyPrivacyButton_Click(object sender, System.EventArgs e)
		{
			SurveyData surveyData = new Surveys().GetSurveyById(SurveyId);
			surveyData.Surveys[0].RedirectionURL = RedirectionURLTextBox.Text;
			surveyData.Surveys[0].ThankYouMessage = ThankYouFreeTextBox.Text.Length > 3900 ?
				ThankYouFreeTextBox.Text.Substring(0, 3900) : ThankYouFreeTextBox.Text;

			// Update the DB
			new Survey().UpdateSurvey(surveyData);

			MessageLabel.Text = GetPageResource("SurveyUpdatedMessage");
			MessageLabel.Visible = true;
		}

	}
}
